#include "Piece.h"
#include"Game.h"
Piece::Piece()
	: m_iSquareIndex(0)
{}

void Piece::Init(int a_iOwner, int a_iTeamIndex)
{
	m_iOwner = a_iOwner;		// set the owner
	m_iType = PIECE;
	m_iTeamIndex = a_iTeamIndex;

	pos.y = 25.0f;
	scale.x = scale.y = scale.z = .7f;
	m_iSquareIndex=0;
	// determine what the mesh index is based off the owner
	if(m_iOwner == FS3)
	{
		m_camPos=set(0.0,200,-500.0);
		scale.x = scale.y = scale.z = .7f;
		m_iMeshIndex = FS3_MESH;
		m_iSquareIndex=34;
	}
	else if(m_iOwner == YBOX)
	{
		m_camPos=set(0.0,200.0,500.0);
		m_iMeshIndex = YBOX_MESH;
		m_iSquareIndex=4;
	}
	else if(m_iOwner == PC)
	{
		m_camPos=set(500.0,200.0,0.0);
		m_iMeshIndex = PC_MESH;
		m_iSquareIndex=19;
	}
	else if(m_iOwner == WEE)
	{
		m_iMeshIndex = WEE_MESH;
		m_camPos=set(-500.0,200.0,0.0);
		m_iSquareIndex=49;
	}

}


XYZ Piece::getPos()
{

	return m_camPos;
}
void Piece::Init(int a_iOwner, int a_iTeamIndex, float x, float z)
{
	Init(a_iOwner, a_iTeamIndex);
	pos.x = x;
	pos.z = z;
	m_speed=.2;
	m_home=true;

}

bool Piece::Move(int a_iMoveBy)
{

	//m_iSquareIndex+=a_iMoveBy;


	//Game::Engine()->m_eState=PLAYER_SWITCH;
	//move the piece until they are the same
	static bool changeMove=false;
	static int tempMove=0;
	//this will move around the corners
	if(changeMove)
	{
		a_iMoveBy=tempMove;
		//changeMove=false;
	}
	//if the piece is on the Ybox side 0-15 then move him only on the x
	if(m_iSquareIndex <g_iYboxSide)
	{
		//quick fix
		if(m_home)
		{
			if(pos.z<Game::Engine()->m_pBoard->m_boardSpots[g_iYboxSide].pos.z)
				pos.z+=1.0f*m_speed;
			if(pos.z>=Game::Engine()->m_pBoard->m_boardSpots[g_iYboxSide].pos.z)// && m_iSquareIndex==(m_iSquareIndex+a_iMoveBy))
			{
				//line up the board and piece
				pos.z=Game::Engine()->m_pBoard->m_boardSpots[g_iYboxSide].pos.z;
				m_home=false;
			}
		}
 		else if(!m_home)
		{


			if(pos.x<Game::Engine()->m_pBoard->m_boardSpots[m_iSquareIndex+a_iMoveBy].pos.x)
				pos.x+=1.0f*m_speed;

			//if my pos and the boardSpot are equal return true 
			if(pos.x>=Game::Engine()->m_pBoard->m_boardSpots[m_iSquareIndex+a_iMoveBy].pos.x)// && m_iSquareIndex==(m_iSquareIndex+a_iMoveBy))
			{
				//line up the board excalty with the piece
				pos.x=Game::Engine()->m_pBoard->m_boardSpots[g_iYboxSide].pos.x;
				//moves person to next side
				if(m_iSquareIndex+a_iMoveBy>g_iYboxSide)
				{
					//change the card to how many moves is left
					tempMove =(a_iMoveBy+m_iSquareIndex)-g_iYboxSide;
					//change move to true so when you get a card it will be the same number
					changeMove=true;
					//your at the corner to go to the next side
					m_iSquareIndex=g_iYboxSide;
				}
				else
				{
					if(changeMove)
					{
						changeMove=false;
						m_iSquareIndex+=tempMove;
						return true;
					}
					else
					{
						m_iSquareIndex+=a_iMoveBy;
						return true;
					}
				}
			}
		}
	}
	//if the piece is on the PC side 15-30 then move only on the z
	else if(m_iSquareIndex <g_iPCSide)
	{
		//quick fix
		if(m_home)
		{
			if(pos.x<Game::Engine()->m_pBoard->m_boardSpots[g_iPCSide].pos.x)
				pos.x+=1.0f*m_speed;
			if(pos.x>=Game::Engine()->m_pBoard->m_boardSpots[g_iPCSide].pos.x)// && m_iSquareIndex==(m_iSquareIndex+a_iMoveBy))
			{
				pos.x=Game::Engine()->m_pBoard->m_boardSpots[g_iPCSide].pos.x;
				m_home=false;
			}
		}
		else if(!m_home)
		{
			if(pos.z>Game::Engine()->m_pBoard->m_boardSpots[m_iSquareIndex+a_iMoveBy].pos.z)
				pos.z-=1.0f*m_speed;

			//if my pos and the boardSpot are equal return true 
			if(pos.z<=Game::Engine()->m_pBoard->m_boardSpots[m_iSquareIndex+a_iMoveBy].pos.z)
			{
				//make it align excatly how the board is
				pos.z=Game::Engine()->m_pBoard->m_boardSpots[g_iPCSide].pos.z;
				//does the corner move
				if(m_iSquareIndex+a_iMoveBy>g_iPCSide)
				{
					tempMove=a_iMoveBy =(a_iMoveBy+m_iSquareIndex)-g_iPCSide;
					changeMove=true;
					m_iSquareIndex=g_iPCSide;
				}
				else
				{
					changeMove=false;
					m_iSquareIndex+=tempMove;
					return true;
				}

			}
		}
	}
		//if the piece is on the FS3 side 31-47 then move only on the -x
	else if(m_iSquareIndex <g_iFS3Side)
	{
		//quick fix
		if(m_home)
		{
			if(pos.z>Game::Engine()->m_pBoard->m_boardSpots[g_iFS3Side].pos.z)
				pos.z-=1.0f*m_speed;
			if(pos.z<=Game::Engine()->m_pBoard->m_boardSpots[g_iFS3Side].pos.z)// && m_iSquareIndex==(m_iSquareIndex+a_iMoveBy))
			{
				//alignment
				pos.z=Game::Engine()->m_pBoard->m_boardSpots[g_iFS3Side].pos.z;
				m_home=false;
			}
		}
		else if(!m_home)
		{
			if(pos.x>Game::Engine()->m_pBoard->m_boardSpots[m_iSquareIndex+a_iMoveBy].pos.x)
				pos.x-=1.0f*m_speed;
			//if my pos and the boardSpot are equal return true 
			if(pos.x<=Game::Engine()->m_pBoard->m_boardSpots[m_iSquareIndex+a_iMoveBy].pos.x)// && m_iSquareIndex==(m_iSquareIndex+a_iMoveBy))
			{
				//alignment
				pos.x=Game::Engine()->m_pBoard->m_boardSpots[g_iFS3Side].pos.x;
				//does the corner
				if(m_iSquareIndex+a_iMoveBy>g_iFS3Side)
				{
					tempMove=a_iMoveBy =(a_iMoveBy+m_iSquareIndex)-g_iFS3Side;
					changeMove=true;
					m_iSquareIndex=g_iFS3Side;
				}
				else
				{
					changeMove=false;
					m_iSquareIndex+=tempMove;
					return true;
				}

			}
		}
	}
		//if the piece is on the PC side 47-64 then move only on the z+
	else if(m_iSquareIndex <=g_iWeeSide)
	{
		//quick fix
		if(m_home)
		{
			if(pos.x>Game::Engine()->m_pBoard->m_boardSpots[g_iWeeSide].pos.x)
				pos.x-=1.0f*m_speed;
			if(pos.x<=Game::Engine()->m_pBoard->m_boardSpots[g_iWeeSide].pos.x)// && m_iSquareIndex==(m_iSquareIndex+a_iMoveBy))
			{
				//alignment
				pos.x=Game::Engine()->m_pBoard->m_boardSpots[g_iWeeSide].pos.x;
				m_home=false;
			}
		}
		else if(!m_home)
		{
			if(pos.z<Game::Engine()->m_pBoard->m_boardSpots[m_iSquareIndex+a_iMoveBy].pos.z)
				pos.z+=1.0f*m_speed;
			//if my pos and the boardSpot are equal return true 
			if(pos.z>=Game::Engine()->m_pBoard->m_boardSpots[m_iSquareIndex+a_iMoveBy].pos.z)// && m_iSquareIndex==(m_iSquareIndex+a_iMoveBy))
			{
				//alignment
				pos.z=Game::Engine()->m_pBoard->m_boardSpots[g_iFS3Side].pos.z;
				//quick fix
				if(m_iSquareIndex+a_iMoveBy>g_iWeeSide)
				{
					tempMove=a_iMoveBy =g_iWeeSide-(a_iMoveBy+m_iSquareIndex);
					changeMove=true;
					//zero because it cycles
					m_iSquareIndex=0;
				}
				else
				{
					changeMove=false;
					m_iSquareIndex+=tempMove;
					return true;
				}
			}
		}
	}
	//else if(m_iSquareIndex ==60)
	//{
	//	m_iSquareIndex=0;
	//}

	return false;
}

Piece::~Piece()
{
}